package cn.lll.vuehouse.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.dto.GoodsPageDTO;
import cn.lll.vuehouse.entity.Goods;
import cn.lll.vuehouse.entity.Provider;
import cn.lll.vuehouse.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * @author: Cong
 * @date: 2023/9/5
 * @version: 1.0
 * @description:
 */
import cn.lll.vuehouse.common.ResponseResult;
import cn.lll.vuehouse.common.ResponseStatus;
import cn.lll.vuehouse.entity.Goods;
import cn.lll.vuehouse.mapper.GoodsMapper;
import cn.lll.vuehouse.service.CustomerService;
import cn.lll.vuehouse.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    @RequestMapping("page")
    public ResponseResult getPageList(GoodsPageDTO goodsPageDTO) {
        return goodsService.getGoodsPage(goodsPageDTO);
    }

    @PutMapping("updateAvailable")
    public ResponseResult updateState(@RequestBody Goods goods) {
        goodsService.updateById(goods);
        ResponseResult responseResult = new ResponseResult();
        responseResult.success();
        return responseResult;
    }

    @GetMapping("getById")
    public ResponseResult getGoodsInfo(Integer id) {
        return goodsService.getGoodsById(id);
    }

    // 添加和编辑请求
    @PostMapping("addOrUpdate")
    public ResponseResult addOrUpdateGoods(@RequestBody Goods goods) {
        if (ObjectUtil.isNotNull(goods.getId())) {
            return goodsService.updateGoods(goods);
        }
        return goodsService.addGoods(goods);
    }

    @DeleteMapping("batchDelete")
    public ResponseResult batchDelete(@RequestBody Integer[] ids) {
        ResponseResult responseResult = new ResponseResult();
        List<Integer> list = Arrays.asList(ids);
        goodsService.removeBatchByIds(list);
        responseResult.success();
        return responseResult;
    }


    @GetMapping("principal")
    public ResponseResult principal(){
        List<Goods> list = goodsService.list();
        ResponseResult responseResult = new ResponseResult();
        responseResult.success(ResponseStatus.SUCCESS,list);
        return responseResult;
    }
}
